package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.HospInfoApplication;
import com.dhcc.DRGsHosp.core.domain.HospInfo;

@Named
@Transactional
public class HospInfoApplicationImpl implements HospInfoApplication {

	@Autowired  
    private JdbcTemplateObj jdbcTemplateObj; 
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
    private EntityRepository getRepository() {
		EntityRepository repository = null;
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
    
	public HospInfo getHospInfo(String hospCode) {
		return HospInfo.get(HospInfo.class, hospCode);
	}
	
	public void creatHospInfo(HospInfo hosp) {
		hosp.save();
	}
	
	public void updateHospInfo(HospInfo hosp) {
		hosp.save();
	}
	
	public void removeHospInfo(HospInfo hosp) {
		if(hosp != null){
			hosp.remove();
		}
	}
	
	public void removeHospInfos(Set<HospInfo> hosps) {
		for (HospInfo hosp : hosps) {
			hosp.remove();
		}
	}

	/**  
	 *  @Description：获取全部院区信息
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.HospInfoApplication#findAllHosp()  
	 */
	@Override
	public List<HospInfo> findAllHosp() {
		return HospInfo.findAll(HospInfo.class);
	}

	/**
     *  
     *  isHospOrderExist.  
     *  @Description：院区编码是否存在
     *  @Author fulianxia
     *  @param hospOrder
     *  @return 
     *  @Date:2018年3月26日
     */
	@Override
	public boolean isHospOrderExist(String hospOrder,String hospCode) {
		String sql = "select count(1) from drgs.t_hospinfo t where t.hosp_order = '"+hospOrder+"' " ;
		if(hospCode != null){
			sql += " and t.hosp_code <> '"+hospCode+"'";
		}
		Integer count = Integer.parseInt(getQueryChannelService().createSqlQuery(sql).singleResult().toString()) ;
		if( count == 0 ) {
			return false ;
		} 
		return true;
	}
	
}
